#include "config.h"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include "aux_types.h"
#include "aux_errno.h"
#include "aux_process.h"
#include "linux_process.h"

EErrNo xIPCLinuxCreateBackgroundProcess(TxIPCProcess* ptProcess)
{
    pid_t tPid;

    if (!ptProcess || !(ptProcess->pszExePath))
        return -ERRNO_EPARAMETER_INVALID;

    if (access(ptProcess->pszExePath, X_OK))
        return -ERRNO_EPARAMETER_INVALID;

    tPid = fork();
    if (tPid < 0)
    {
        return ERRNO_FAIL;
    }
    else if (tPid > 0)
    {
        ptProcess->nProcessId = tPid;
        return ERRNO_SUCCESS;
    }
    else
    {
        if (execl(ptProcess->pszExePath, ptProcess->pszParameters, (char *)NULL))
        {
            exit(0);
        }
    }

}


EErrNo xIPCLinuxGetProcessInterface(TxIPCProcessInterface *pInterface)
{
    if (!pInterface)
        return ERRNO_FAIL;

    memset(pInterface, 0, sizeof(TxIPCProcessInterface));

    pInterface->CreateBackground = xIPCLinuxCreateBackgroundProcess;

    return ERRNO_SUCCESS;
}


